/*
* © 2010 Autodata Solutions Company. All Rights Reserved
* This source code is the confidential and proprietary information of Autodata Solutions Company.
* The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
* without the prior written consent of Autodata Solutions Company.
*/
Ext.ns('net.autodata.compare.photos.plugins');
(function() {

    /**
    * The purpose of this plugin is to connect the PhotoPanel.vehiclePhotosPanel's viewlarger event to
    * the display of the PhotoWindow
    * @class PhotosPanelPhotoWindowConnector
    * @extends Object
    * @namespace net.autodata.compare.photos.plugins
    */
    var Plugin = Ext.extend(Object, {

        /**
        * @property photosPanel
        * @type net.autodata.compare.photos.PhotosPanel
        */
        photosPanel: undefined,

        /**
        * The init method is called by the plugin architecture of the Ext.Component
        * @method init
        * @param {net.autodata.compare.photos.PhotosPanel} photosPanel
        */
        init: function(photosPanel) {
            this.photosPanel = photosPanel;
            photosPanel.on('destroy', this.onDestroy, this);
            photosPanel.vehiclePhotos.on('viewlarger', this.onViewLarger, this);
        },

        /**
        * @private
        */
        onViewLarger: function(acode, shotId, imageType) {
            var store = this.photosPanel.getStore();
            var records = store.getRawData('acode', acode, false, false);

            var imageId = String.format('{0}-{1}-{2}', acode, shotId, imageType);

            var vehicle = this.photosPanel.getVehicle(acode);
            var toolbar = new net.autodata.compare.toolbar.PhotoSelectionToolbar({
                pagerTpl: ASC.message('net.autodata.compare.toolbar.pagination.template'),
                vehicleInfo: {
                    modelYear: vehicle.modelYear.description,
                    make: vehicle.make.description,
                    model: vehicle.model.description,
                    trim: vehicle.trim.description,
                    msrp: vehicle.msrp
                }
            });

            var wnd = new net.autodata.compare.photos.PhotoWindow({
                records: records,
                startIndex: records.findIndex('imageId', imageId, false, true),
                closeAction: 'close',
                bbar: toolbar,
                plugins: [toolbar]
            });

            wnd.show();
        },

        /**
        * @private
        */
        onDestroy: function() {
            var photosPanel = this.photosPanel
            photosPanel.vehiclePhotos.un('viewlarger', this.onViewLarger, this);
            photosPanel.un('render', this.onRender, this);
            photosPanel.un('destroy', this.onDestroy, this);
        }
    });

    net.autodata.compare.photos.plugins.PhotosPanelPhotoWindowConnector = Plugin;

})();